package com.itheima.thread.Demo5;

public class SynchronizedMethod {
    private static int ticket = 100;
    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
           while(true){
               boolean res = sellticket();
               if(res){
                   break;
               }
           }
        });
        t1.setName("窗口一");
        t1.start();

        Thread t2 = new Thread(()->{
           while(true){
               synchronized (SynchronizedMethod.class) {
                   if(ticket == 0){
                       break;
                   }else{
                       ticket--;
                       try {
                           Thread.sleep(10);
                       } catch (InterruptedException e) {
                           e.printStackTrace();
                       }
                       System.out.println(Thread.currentThread().getName()+"正在买票,剩余票数:"+ticket+"张");
                   }
               }
           }
        });
        t2.setName("窗口二");
        t2.start();
    }
    public static synchronized boolean sellticket(){
        if(ticket == 0){
            return true;
        }else{
            ticket--;
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"正在买票,剩余票数:"+ticket+"张");
            return false;
        }
    }
}
